package com.me.utils;

import android.content.Context;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

import com.me.blood.activity.WebActivity;

import java.lang.ref.WeakReference;

public class MyClickableSpan extends ClickableSpan {

    private final String content;
    private final WeakReference<Context> context;
    private final int color;

    public MyClickableSpan(Context context, String content) {
        this(context, content, 0);
    }

    public MyClickableSpan(Context context, String content, int color) {
        this.content = content;
        this.context = new WeakReference<>(context);
        this.color = color;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(true);
        if (context.get() != null) {
            ds.setColor(color == 0 ? 0xff6855FF : color);
        }
    }

    @Override
    public void onClick(View widget) {
        if (context.get() == null) {
            return;
        }
        WebActivity.launch(widget.getContext(), content);
    }
}
